package com.cn.web.controller.admin;


import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.bc.Menu;
import com.cn.config.config.Msg;
import com.cn.service.server.admin.AdminMenuServer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class AdminMenuController {
    private final AdminMenuServer adminMenuServer;
    @Autowired
    public AdminMenuController(AdminMenuServer adminMenuServer) {
        this.adminMenuServer = adminMenuServer;
    }
    @RequestMapping("/select/Admin/Menu")
    @ResponseBody
    public List<Menu> selectAdminRole(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String menuId = jsonObject.getString("menuId");
        String title = jsonObject.getString("title");
        menuId = (menuId != null) ? menuId : "";
        title = (title != null) ? title : "";
        List<Menu> menus = adminMenuServer.selectAdminMenu(menuId, title, limit, page);
        return menus;
    }
    @RequestMapping("/insert/Admin/Menu")
    @ResponseBody
    public Msg insertAdminRole(@RequestBody JSONObject jsonObject) {
        Msg msg=new Msg();
        int level = jsonObject.getInteger("level");
        int parentId = jsonObject.getInteger("parentId");
        String menuName = jsonObject.getString("menuName");
        String title = jsonObject.getString("title");
        String index = jsonObject.getString("index");
        String remark = jsonObject.getString("remark");

        int menuId = adminMenuServer.getAutoId()+1;
        int i = adminMenuServer.insertAdminMenu(menuId,level,parentId,menuName,remark,index,title);
        if (i>0){
            msg.setCode(200);
            msg.setMessage("保存成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("保存失败");
        return msg;
    }
    @RequestMapping("/update/Admin/Menu")
    @ResponseBody
    public Msg updateAdminMenu(@RequestBody JSONObject jsonObject) {
        Msg msg=new Msg();
        int menuId = jsonObject.getInteger("menuId");
        int level = jsonObject.getInteger("level");
        int parentId = jsonObject.getInteger("parentId");
        String menuName = jsonObject.getString("menuName");
        String title = jsonObject.getString("title");
        String index = jsonObject.getString("index");
        String remark = jsonObject.getString("remark");
        int i = adminMenuServer.updateAdminMenu(menuId,level,parentId,menuName,remark,index,title);
        if (i>0){
            msg.setCode(200);
            msg.setMessage("修改成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("修改失败");
        return msg;
    }
    @RequestMapping("/delete/Admin/Menu")
    @ResponseBody
    public Msg deleteAdminMenu(@RequestBody JSONObject jsonObject) {
        Integer menuId = jsonObject.getInteger("menuId");
        Msg msg=new Msg();
        int i = adminMenuServer.deleteAdminMenu(menuId);
        if (i>0){
            msg.setCode(200);
            msg.setMessage("删除成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("删除失败");
        return msg;
    }
}
